package com.third.customer.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.third.customer.model.dao.CompanyDao;
import com.third.customer.model.dao.UserDao;
import com.third.customer.model.dao.param.UserParam;
import com.third.customer.model.tables.User;
import com.third.customer.service.UserService;
import com.third.customer.service.client.RedisClient;
import com.third.customer.service.data.UserModifyReq;
import com.third.customer.service.data.UserReq;
import com.third.customer.service.exception.ThirdBadRequestException;
import com.third.customer.service.exception.ThirdNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

/**
 * Created by hehuaichun on 2020/5/19.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private CompanyDao companyDao;

    @Autowired
    private RedisClient redisClient;

    @Override
    public User open(Long companyId, UserReq req) {
        companyDao.getById(companyId).orElseThrow(() ->
                new ThirdBadRequestException("暂不支持当前公司用户注册!"));
        Optional<User> optional = userDao.getByName(companyId, req.getName());
        if (optional.isPresent()) {
            throw new ThirdBadRequestException("该用户名已被注册!");
        }
        User user = new User();
        user.setName(req.getName());
        user.setCompanyId(companyId);
        user.setPassword(req.getPassword());
        user.setType(req.getType().getCode());
        userDao.insert(user);
        return user;
    }

    @Override
    public User modify(Long userId, UserModifyReq req) {
        User user = new User();
        user.setId(userId);
        user.setName(req.getName());
        if(!StringUtils.isEmpty(req.getPassword())) {
            user.setPassword(req.getPassword());
        }
        userDao.update(user);
        return user;
    }

    @Override
    public boolean delete(Long userId) {
        userDao.getById(userId).orElseThrow(() -> new ThirdNotFoundException("用户不存在"));
        return userDao.deleteById(userId);
    }

    @Override
    public User getById(Long userId) {
        return userDao.getById(userId).orElseThrow(() -> new ThirdNotFoundException("用户不存在"));
    }

    @Override
    public User getByName(Long companyId, String name) {
        return userDao.getByName(companyId, name).orElseThrow(() -> new ThirdNotFoundException("用户不存在"));
    }

    @Override
    public Page<User> list(UserParam param) {
        return userDao.query(param);
    }

    @Override
    public String login(Long companyId, String name, String password) {
        User user = getByName(companyId, name);
        if (!Objects.equals(user.getPassword(), password)) {
            throw new ThirdBadRequestException("用户密码不正确!");
        }
        String token = UUID.randomUUID().toString().replace("-", "").toUpperCase();
        //todo 存在多次登录,多个token的问题,待优化
        redisClient.setLoginToken(companyId, name, token, true);
        return token;
    }

    @Override
    public boolean logout(String token) {
        return redisClient.removeLoginToken(token);
    }
}
